package action.AB01;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import services.AB01.AB01DWServices;
import services.login.LoginServices;
import com.*;
import com.syslog.SysLogServices;
import com.util.Component;
import com.util.Util;
/**
* PersonMgrFacade:单位信息action
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
@Controller
@RequestMapping("unit.do")
public class AB01DWAction {
	PublicSystem sys = PublicSystem.getInstance();
	AB01DWServices ss = new AB01DWServices();
	SysLogServices log = SysLogServices.getInstance();
	LoginServices ls = new LoginServices();
	/**
	 * 查询包括注销所有单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunit")
	public void getUnit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key1 = Component.getQuest("key1", request);
		String key2 = Component.getQuest("key2", request);
		String key3 = Component.getQuest("key3", request);
		HashMap parms = sys.getGridSort(request);
		String aae011 = Component.getSession("userid", request);
		String json = Util.Encode(ss.getUnit(new Object[] {key1, key2, key3}, parms));
		HashMap obj = new HashMap();
		obj.put("domainid", "ZYK");    /*分系统号，见sys_log_bm*/
		obj.put("log_type", "T05");    /*日志类别，见sys_log_bm*/
		obj.put("userid", aae011);     /*操作人*/
		obj.put("rwh", Component.getSession("rwid", request));	/*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
		obj.put("content", "单位资源库信息查询，劳动保障证号：" + key1 + "，单位名称为：" + key2);
		obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
		obj.put("czrlb", "L01");       /*操作人类别，见sys_log_bm*/
		obj.put("datain", key1 + "," + key2); /*//输入参数*/
		obj.put("dataout", "XCOk");    /*输出参数*/
		log.sysLogMap(obj, request);
		Component.print(json, response);
	} 
	/**
	 * 查询不包括注销所有单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunit1")
	public void getUnit1 (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key1 = Component.getQuest("key1", request);
		String key2 = Component.getQuest("key2", request);
		HashMap parms = sys.getGridSort(request);
		String aae011 = Component.getSession("userid", request);
		String json = Util.Encode(ss.getUnit1(new Object[] {key1, key2}, parms));
		HashMap obj = new HashMap();
		obj.put("domainid", "ZYK");    /*分系统号，见sys_log_bm*/
		obj.put("log_type", "T05");    /*日志类别，见sys_log_bm*/
		obj.put("userid", aae011);     /*操作人*/
		obj.put("rwh", Component.getSession("rwid", request));	/*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
		obj.put("content", "单位资源库信息查询，劳动保障证号：" + key1 + "，单位名称为：" + key2);
		obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
		obj.put("czrlb", "L01");       /*操作人类别，见sys_log_bm*/
		obj.put("datain", key1 + "," + key2); /*//输入参数*/
		obj.put("dataout", "XCOk");    /*输出参数*/
		log.sysLogMap(obj, request);
		Component.print(json, response);
	} 
	/**
	 * 查询选中单位的基本信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitdata")
	public void getUnitData(HttpServletRequest request, HttpServletResponse response) throws Exception {	
		HashMap user = ss.getUnitData(Component.getQuest("id", request));
		String aab001 = Component.getQuest("id", request);
		HashMap obj = new HashMap();
		obj.put("domainid", "ZYK");    /*分系统号，见sys_log_bm*/
		obj.put("log_type", "T04");    /*日志类别，见sys_log_bm*/
		obj.put("userid", Component.getSession("userid", request));   /*操作人*/
		obj.put("rwh", Component.getSession("rwid", request));	      /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
		obj.put("content", "单位资源库注销，该劳动保障证号为：" + aab001);
		obj.put("czqd", "Q1");	      /*操作渠道，见sys_log_bm*/
		obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
		obj.put("datain", aab001);    /*输入参数*/
		obj.put("dataout", "ZXOk");   /*输出参数*/
		log.sysLogMap(obj, request);
		Component.print(Util.Encode(user), response);
	} 
	/**
	 * 查询选中单位的基本信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitdata1")
	public void getUnitData1(HttpServletRequest request, HttpServletResponse response) throws Exception {	
		HashMap user = ss.getUnitData1(Component.getQuest("id", request));
		String aab001 = Component.getQuest("id", request);
		HashMap obj = new HashMap();
		obj.put("domainid", "ZYK");    /*分系统号，见sys_log_bm*/
		obj.put("log_type", "T04");    /*日志类别，见sys_log_bm*/
		obj.put("userid", Component.getSession("userid", request));   /*操作人*/
		obj.put("rwh", Component.getSession("rwid", request));	      /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
		obj.put("content", "单位资源库注销，该劳动保障证号为：" + aab001);
		obj.put("czqd", "Q1");	      /*操作渠道，见sys_log_bm*/
		obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
		obj.put("datain", aab001);    /*输入参数*/
		obj.put("dataout", "ZXOk");   /*输出参数*/
		log.sysLogMap(obj, request);
		Component.print(Util.Encode(user), response);
	} 
	/**
	 * 判断单位名是否重复
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitname")
	public void getUnitName(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String json = new String(request.getParameter("name").getBytes("iso8859-1"), "utf-8");
		HashMap name = ss.getUnitName(json);
		Component.print(Util.Encode(name), response);
	}
	/**
	 * 判断统一社会信用编码是否重复
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitcreditid")
	public void getUnitCreditId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String json = new String(request.getParameter("id").getBytes("iso8859-1"), "utf-8");
		HashMap id = ss.getUnitCreditId(json);
		Component.print(Util.Encode(id), response);
	}
	/**
	 * 编辑界面判断组织机构代码是否重复
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitorganizationid")
	public void getUnitOrganizationId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String json = new String(request.getParameter("id").getBytes("iso8859-1"), "utf-8");
		String json1 = new String(request.getParameter("aab001").getBytes("iso8859-1"), "utf-8");
		HashMap id = ss.getUnitOrganizationId(json, json1);
		Component.print(Util.Encode(id), response);
	}
	/**
	 * 新增界面判断组织机构代码是否重复
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getunitorganizationids")
	public void getUnitOrganizationIds(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String json = new String(request.getParameter("id").getBytes("iso8859-1"), "utf-8");
		HashMap id = ss.getUnitOrganizationIds(json);
		Component.print(Util.Encode(id), response);
	}
	/**
	 * 保存页面单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=saveunit")
	public void saveUnit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String idreturn = "";
		String json = request.getParameter("data");
		ArrayList rows = (ArrayList)Util.Decode(json);
		HashMap row = (HashMap)rows.get(0);
		String id = row.get("aab001") != null ? row.get("aab001").toString() : "";
		String aae011 = Component.getSession("userid", request);
		String aab620 = Component.getQuest("aab620", request);
		row.put("aae011", aae011);
		if(!"".equals(id)) {
			HashMap map = ss.getAb01Datas((String)row.get("aab001"));
			Iterator<Entry<String, String>> iter1 = map.entrySet().iterator();
		    String str4 = "";
	        while(iter1.hasNext()) {
	            Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
	            String mapvalue = entry1.getValue() == null ? "":entry1.getValue();
	            String rowvalue = row.get(entry1.getKey()) == null?"":(String)row.get(entry1.getKey());
	            if (!mapvalue.equals(rowvalue)) {			/*若两个map中相同key对应的value不相等*/   
	                HashMap row1 = new HashMap();
	                String str3 = "修改字段为："+entry1.getKey() + "变更前：" + mapvalue+"变更后：" + rowvalue;
	                str4 += str3;
	            }
	        }
	        row.put("czlx", "2");
			idreturn = ss.checkData(row);
			if("0".equals(idreturn)) {
				String dateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
				row.put("aae035", dateStr);
				idreturn = ss.updateData(row);
				HashMap obj = new HashMap();
		  		obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
		  		obj.put("log_type", "T03");   /*日志类别，见sys_log_bm*/
	  		    obj.put("userid",aae011);     /*操作人*/
	  		    obj.put("rwh", Component.getSession("rwid", request));	   /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/   
	  		    obj.put("content",str4);      /*日志内容，自己组合拼装*/
	  		    obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
	  		    obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
	  		    String strs2 = row.get("aab004") + "," + row.get("aab019") + "," + row.get("aae004") + "," + row.get("aae005") + "," + row.get("aab013")+"," + row.get("aae018") + "," + row.get("aab014") + ","+row.get("aab015");
	  		    obj.put("datain", strs2 + "," + aab620 + "," + row.get("aab523"));   /*输入参数*/
	  		    obj.put("dataout", "updateOk");   /*输出参数*/
	  		    log.sysLogMap(obj, request);
	  		    Component.print(idreturn, response);
				return;
			} else {
				Component.print(idreturn, response);
				return;
			}
	    } else {
			idreturn = ss.insertUnit(row);
			HashMap obj = new HashMap();
    		obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
    		obj.put("log_type", "T02");   /*日志类别，见sys_log_bm*/
    		obj.put("userid", aae011);    /*操作人*/
    		obj.put("rwh", Component.getSession("rwid", request));  /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
    		obj.put("content", "单位资源库登记成功， "+"劳动保障证号为："+ idreturn +"，单位名称为："+row.get("aab004"));  /*日志内容，自己组合拼装*/
    		obj.put("czqd", "Q1");	      /*操作渠道，见sys_log_bm*/
    		obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
    		obj.put("datain", "单位名称：" + row.get("aab004") + "，单位性质：" + row.get("aab019") + "，法定代表人证件号码：" + row.get("aab014")); /*输入参数*/
    		obj.put("dataout", "InsertOk");   /*输出参数*/
    		log.sysLogMap(obj,request);
		}
	    Component.print(idreturn, response);
	} 
	/**
	 * 保存页面单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=update")
	public void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String idreturn = "";
		String json = request.getParameter("data");
		ArrayList rows = (ArrayList)Util.Decode(json);
		HashMap row = (HashMap)rows.get(0);
		String id = row.get("aab001") != null ? row.get("aab001").toString() : "";
		String aae011 = Component.getSession("userid", request);
		String aab620 = Component.getQuest("aab620", request);
		row.put("aae011", aae011);
		if(!"".equals(id)) {
			HashMap map = ss.getAb01Datas((String)row.get("aab001"));
			Iterator<Entry<String, String>> iter1 = map.entrySet().iterator();
		    String str4 = "";
	        while(iter1.hasNext()) {
	            Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
	            String mapvalue = entry1.getValue() == null?"":entry1.getValue();
	            String rowvalue = row.get(entry1.getKey()) == null ? "":(String)row.get(entry1.getKey());
	            if (!mapvalue.equals(rowvalue)) {			/*若两个map中相同key对应的value不相等*/   
	                HashMap row1 = new HashMap();
	                String str3 = "修改字段为：" + entry1.getKey() + "变更前：" + mapvalue + "变更后：" + rowvalue;
	                str4 += str3;
	            }
	        }
	        row.put("czlx", "2");
				String dateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
				row.put("aae035", dateStr);
				idreturn = ss.updateData(row);
				 HashMap obj = new HashMap();
		  		 obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
		  		 obj.put("log_type", "T03");   /*日志类别，见sys_log_bm*/
	  		     obj.put("userid", aae011);     /*操作人*/
	  		     obj.put("rwh", Component.getSession("rwid", request));	   /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/   
	  		     obj.put("content", str4);      /*日志内容，自己组合拼装*/
	  		     obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
	  		     obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
	  		     String strs2 = row.get("aab004") + "," + row.get("aab019") + "," + row.get("aae004") + ","+row.get("aae005") + "," + row.get("aab013") + "," + row.get("aae018")+"," + row.get("aab014") + "," + row.get("aab015");
	  		     obj.put("datain", strs2 + "," + aab620 + "," + row.get("aab523"));   /*输入参数*/
	  		     obj.put("dataout", "updateOk");   /*输出参数*/
	  		     log.sysLogMap(obj, request);
	  		     Component.print(idreturn, response);
				 return;
	    } 
	    Component.print(idreturn, response);
	} 
	/**
	 * 保存页面单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=saveunit1")
	public void saveUnit1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String idreturn = "";
		String json = request.getParameter("data");
		ArrayList rows = (ArrayList)Util.Decode(json);
		HashMap row = (HashMap)rows.get(0);
		String id = row.get("aab001") != null ? row.get("aab001").toString() : "";
		String aae011 = Component.getSession("userid", request);
		String aab620 = Component.getQuest("aab620", request);
		row.put("aae011", aae011);
		if(!"".equals(id)) {
			HashMap map = ss.getAb01Datas((String)row.get("aab001"));
			Iterator<Entry<String, String>> iter1 = map.entrySet().iterator();
		    String str4 = "";
	        while(iter1.hasNext()){
	            Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
	            String mapvalue = entry1.getValue() == null?"":entry1.getValue();
	            String rowvalue = row.get(entry1.getKey()) == null?"":(String)row.get(entry1.getKey());
	            if (!mapvalue.equals(rowvalue)) {			/*若两个map中相同key对应的value不相等*/   
	                HashMap row1 = new HashMap();
	                String str3 = "修改字段为：" + entry1.getKey() + "变更前：" + mapvalue + "变更后：" + rowvalue;
	                str4 += str3;
	            }
	        }
			idreturn = ss.modifiedUnit(row);
			if("".equals(idreturn)){
				HashMap obj = new HashMap();
		  		obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
		  		obj.put("log_type", "T03");   /*日志类别，见sys_log_bm*/
	  		    obj.put("userid", aae011);     /*操作人*/
	  		    obj.put("rwh", Component.getSession("rwid", request));	   /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/   
	  		    obj.put("content", str4);      /*日志内容，自己组合拼装*/
	  		    obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
	  		    obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
	  		    String strs2 = row.get("aab004") + "," + row.get("aab019") + "," + row.get("aae004") + ","+row.get("aae005") + "," + row.get("aab013") + "," + row.get("aae018") + "," + row.get("aab014") + "," + row.get("aab015");
	  		    obj.put("datain", strs2 + "," + aab620 + "," + row.get("aab523"));   /*输入参数*/
	  		    obj.put("dataout", "updateOk");   /*输出参数*/
	  		    log.sysLogMap(obj, request);
			}
	    } else {
			idreturn = ss.insertUnit1(row);
			HashMap obj = new HashMap();
    		obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
    		obj.put("log_type", "T02");   /*日志类别，见sys_log_bm*/
    		obj.put("userid", aae011);    /*操作人*/
    		obj.put("rwh", Component.getSession("rwid", request));  /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/
    		obj.put("content", "单位资源库登记成功， " + "劳动保障证号为：" + idreturn + "，单位名称为：" + row.get("aab004"));  /*日志内容，自己组合拼装*/
    		obj.put("czqd", "Q1");	      /*操作渠道，见sys_log_bm*/
    		obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
    		obj.put("datain", "单位名称：" + row.get("aab004") + "，单位性质：" + row.get("aab019") + "，法定代表人证件号码：" + row.get("aab014")); /*输入参数*/
    		obj.put("dataout", "InsertOk");   /*输出参数*/
    		log.sysLogMap(obj,request);
		}
	    Component.print(idreturn, response);
	} 
	/**
	 * 单位性质 法定代表人证件类型的下拉框选项
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getselect")
	public void getSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List data = ss.getSelect(Component.getQuest("lx", request));
		Component.print(Util.Encode(data), response);
	}		
	/**
	 * 注销原因的下拉框选项
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getselect1")
	public void getSelect1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List data = ss.getSelect1(Component.getQuest("lx", request));
		Component.print(Util.Encode(data), response);
	}		
	/**
	 * 校验身份证
	 * @author bjh
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=checksfz")
	public void checksfz(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String aac002 = Component.getQuest("id", request);
		HashMap map = new HashMap();
		map.put("aac002", aac002);
		String res = ss.checksfz(map);
	    Component.print(res, response);
	}
	/**
	 * 保存页面单位信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=cancel")
	public void cancel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String idreturn = "";
		String json = request.getParameter("data");
		ArrayList rows = (ArrayList)Util.Decode(json);
		HashMap row = (HashMap)rows.get(0);
		String id = row.get("aab001") != null ? row.get("aab001").toString() : "";
		String aae011 = Component.getSession("userid", request);
		String aab620 = Component.getQuest("aab620", request);
		String aab523 = Component.getQuest("aab523", request);
		row.put("aae011", aae011);
		if(!"".equals(id)) {
			HashMap map = ss.getAb01Datas((String)row.get("aab001"));
			Iterator<Entry<String, String>> iter1 = map.entrySet().iterator();
		    String str4 = "";
	        while(iter1.hasNext()) {
	            Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
	            String mapvalue = entry1.getValue() == null?"":entry1.getValue();
	            String rowvalue = row.get(entry1.getKey()) == null?"":(String)row.get(entry1.getKey());
	            if (!mapvalue.equals(rowvalue)) {			/*若两个map中相同key对应的value不相等*/   
	                HashMap row1 = new HashMap();
	                String str3 = "修改字段为：" + entry1.getKey() + "变更前：" + mapvalue + "变更后：" + rowvalue;
	                str4 += str3;
	            }
	        }
	        String str = (String) row.get("aab523");
	        //String str = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
	        row.put("aae035", str);
	        idreturn =  ss.cancel(row);
				 HashMap obj = new HashMap();
		  		 obj.put("domainid", "ZYK");   /*分系统号，见sys_log_bm*/
		  		 obj.put("log_type", "T03");   /*日志类别，见sys_log_bm*/
	  		     obj.put("userid", aae011);     /*操作人*/
	  		     obj.put("rwh", Component.getSession("rwid", request));	   /*任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义*/   
	  		     obj.put("content", str4);      /*日志内容，自己组合拼装*/
	  		     obj.put("czqd", "Q1");	       /*操作渠道，见sys_log_bm*/
	  		     obj.put("czrlb", "L01");      /*操作人类别，见sys_log_bm*/
	  		     String strs2 = row.get("aab004") + "," + row.get("aab019") + "," + row.get("aae004") + "," + row.get("aae005") + "," + row.get("aab013") + "," + row.get("aae018") + "," + row.get("aab014") + "," + row.get("aab015");
	  		     obj.put("datain",strs2 + "," + aab620 + "," + row.get("aab523"));   /*输入参数*/
	  		     obj.put("dataout", "updateOk");   /*输出参数*/
	  		     log.sysLogMap(obj, request);
	  		     Component.print(idreturn, response);
				 return;
	    } 
	    Component.print(idreturn, response);
	} 
}

